package operation;

import Book.Book;
import Book.BookList;
import com.sun.istack.internal.localization.NullLocalizable;

import java.net.BindException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: zjc
 * Date: 2022-09-18
 * Time: 18:21
 */
public class Delete implements IOoperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入删除图书名称：");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();
        int index = -1;
        int i = 0;
        for (i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                index = i;
                break;
                }
            }

        //1.i>=currentSize 没有这本书
        if(i >= currentSize){
            System.out.println("没有这本书");
            return;
        }

        //2.break; 说明有这本书，书的下标 存储在index里

        for (int j = index; j < currentSize-1; j++) {
            Book book = bookList.getBook(j+1);
            bookList.setBooks(j,book);
        }
        bookList.setBooks(currentSize, null);//最后的地方置为空
        bookList.setUsedSize(currentSize-1);//有效书籍少一个
        System.out.println("删除成功！");
    }
}
